package com.example.guessgame;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener,
		OnItemClickListener {

	private static final String	TAG				= MainActivity.class
														.getSimpleName();
	private ImageView			mIv;

	String						str				= "且前面没有指导价经销商价格蓝精灵和万字之高达变暗";
	private String[]			mChars;
	private List<Integer>		mIndexList;
	String						answerStr		= "高达";
	public int					count			= 0;
	public int					answerLength	= 2;
	private LinearLayout		mTextLayout;
	private ArrayList<TextView>	mTextList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initData();
		initView();
	}

	private void initView() {
		mIv = (ImageView) findViewById(R.id.imageview);
		mTextLayout = (LinearLayout) findViewById(R.id.texts_layout);

		GridView gridView = (GridView) findViewById(R.id.gv_btns);
		CharsAdapter adapter = new CharsAdapter(this, mChars, answerStr);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(this);

		Button mBtnGfrag = (Button) findViewById(R.id.btn_gfrag);
		Button mBomb = (Button) findViewById(R.id.btn_bomb);
		mBtnGfrag.setOnClickListener(this);
		mBomb.setOnClickListener(this);

		for (int i = 0; i < answerStr.length(); i++) {
			TextView tvAns = (TextView) getLayoutInflater().inflate(
					R.layout.item_answer, null);
			tvAns.setGravity(Gravity.CENTER);
			android.widget.LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			layoutParams.width = CommonUtils.dip2px(this, 30);
			layoutParams.height = CommonUtils.dip2px(this, 30);
			layoutParams.setMargins(CommonUtils.dip2px(this, 10),
					CommonUtils.dip2px(this, 10), CommonUtils.dip2px(this, 10),
					CommonUtils.dip2px(this, 10));
			tvAns.setLayoutParams(layoutParams);
			tvAns.setBackgroundColor(getResources().getColor(
					android.R.color.darker_gray));
			mTextLayout.addView(tvAns);
			tvAns.setOnClickListener(this);
		}
	}

	private void initData() {
		mChars = new String[str.length()];
		for (int i = 0; i < str.length(); i++) {
			mChars[i] = str.substring(i, i + 1);
		}

		// 将分解后的字符串数组随机打乱
		/*
		 * 在0~19之间取随机数，取到的作为坐标取chars中的字符串
		 * 如果取到重复的值，则继续取下一个值
		 */
		Random random = new Random();
		mIndexList = new ArrayList<Integer>();
		String[] tempArray = new String[str.length()];
		int count = 0;
		while (mIndexList.size() < mChars.length) {
			int randomIndex = random.nextInt(mChars.length);
			if (!mIndexList.contains(randomIndex)) {
				mIndexList.add(randomIndex);
				tempArray[count++] = mChars[randomIndex];
			}
		}
		mChars = tempArray;

		mTextList = new ArrayList<TextView>();
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btn_gfrag:

			break;

		case R.id.btn_bomb:

			break;
		case R.id.tv_answer:
			((TextView) v).setText(null);
			View item = (View) v.getTag();
			item.setVisibility(View.VISIBLE);
			mTextList.remove(((TextView) v));
			count--;
			break;

		}

	}

	public boolean checkAnswer() {
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<mTextLayout.getChildCount();i++){
			sb.append(((TextView)mTextLayout.getChildAt(i)).getText().toString());
		}
		
		return sb.toString().equals(answerStr);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Logger.i(TAG, "text = " + ((TextView) arg1).getText().toString()
				+ ",pos = " + arg2);
		String tag = (String) arg1.getTag();
		TextView childAt = getIdleTextView();
		if (count < answerLength) {
			if (!textIsSet(childAt)) {
				// 没有设置过文字
				childAt.setText(tag);
				childAt.setTag(arg1);
				count++;
				arg1.setVisibility(View.INVISIBLE);
				if (count == answerLength) {
					if (checkAnswer()) {
						Toast.makeText(this, "过关", Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(this, "失败", Toast.LENGTH_LONG).show();
					}
				}

			} else {
				// 已经设置过文字
			}
		}
	}

	private boolean textIsSet(View view) {
		String text = ((TextView) (view)).getText().toString();
		if (text == null || text.equals("")) {
			// 没有设置文字
			return false;
		} else {
			// 已经设置文字
			return true;
		}
	}

	private TextView getIdleTextView() {
		TextView result = null;
		for (int i = 0; i < mTextLayout.getChildCount(); i++) {
			result = (TextView) mTextLayout.getChildAt(i);
			if (result.getText().toString() == null
					|| result.getText().toString().equals("")) {
				return result;
			}
		}
		return null;
	}
}
